use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Orders::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Orders::Id).string().not_null().primary_key())
                    .col(ColumnDef::new(Orders::Number).string().not_null())
                    .col(ColumnDef::new(Orders::CustomerId).string().not_null())
                    .col(ColumnDef::new(Orders::CustomerGroupId).string().not_null())
                    .col(
                        ColumnDef::new(Orders::ShippingAddressId)
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Orders::BillingAddressId).string().not_null())
                    .col(ColumnDef::new(Orders::CustomerName).string().not_null())
                    .col(ColumnDef::new(Orders::Email).string().not_null())
                    .col(ColumnDef::new(Orders::CallingCode).integer().not_null())
                    .col(ColumnDef::new(Orders::Telephone).string().not_null())
                    .col(ColumnDef::new(Orders::Total).decimal_len(16, 4).not_null())
                    .col(ColumnDef::new(Orders::Locale).string().not_null())
                    .col(ColumnDef::new(Orders::CurrencyCode).string().not_null())
                    .col(ColumnDef::new(Orders::CurrencyValue).string().not_null())
                    .col(ColumnDef::new(Orders::Ip).string().not_null())
                    .col(ColumnDef::new(Orders::UserAgent).text().not_null())
                    .col(ColumnDef::new(Orders::Status).string().not_null())
                    .col(
                        ColumnDef::new(Orders::ShippingMethodCode)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Orders::ShippingMethodName)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Orders::ShippingCustomerName)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Orders::ShippingCallingCode)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Orders::ShippingTelephone)
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Orders::ShippingCountry).string().not_null())
                    .col(
                        ColumnDef::new(Orders::ShippingCountryId)
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Orders::ShippingStateId).string().not_null())
                    .col(ColumnDef::new(Orders::ShippingState).string().not_null())
                    .col(ColumnDef::new(Orders::ShippingCity).string().not_null())
                    .col(ColumnDef::new(Orders::ShippingAddress1).string().not_null())
                    .col(ColumnDef::new(Orders::ShippingAddress2).string().not_null())
                    .col(ColumnDef::new(Orders::ShippingZipcode).string().not_null())
                    .col(
                        ColumnDef::new(Orders::BillingMethodCode)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Orders::BillingMethodName)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Orders::BillingCustomerName)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Orders::BillingCallingCode)
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Orders::BillingTelephone).string().not_null())
                    .col(ColumnDef::new(Orders::BillingCountry).string().not_null())
                    .col(ColumnDef::new(Orders::BillingCountryId).string().not_null())
                    .col(ColumnDef::new(Orders::BillingStateId).string().not_null())
                    .col(ColumnDef::new(Orders::BillingState).string().not_null())
                    .col(ColumnDef::new(Orders::BillingCity).string().not_null())
                    .col(ColumnDef::new(Orders::BillingAddress1).string().not_null())
                    .col(ColumnDef::new(Orders::BillingAddress2).string().not_null())
                    .col(ColumnDef::new(Orders::BillingZipcode).string().not_null())
                    .col(ColumnDef::new(Orders::Comment).text())
                    .col(
                        ColumnDef::new(Orders::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Orders::UpdatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Orders::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Orders::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Orders {
    Table,
    Id,
    Number,
    CustomerId,
    CustomerGroupId,
    ShippingAddressId,
    BillingAddressId,
    CustomerName,
    Email,
    CallingCode,
    Telephone,
    Total,
    Locale,
    CurrencyCode,
    CurrencyValue,
    Ip,
    UserAgent,
    Status,
    ShippingMethodCode,
    ShippingMethodName,
    ShippingCustomerName,
    ShippingCallingCode,
    ShippingTelephone,
    ShippingCountry,
    ShippingCountryId,
    ShippingStateId,
    ShippingState,
    ShippingCity,
    ShippingAddress1,
    ShippingAddress2,
    ShippingZipcode,
    BillingMethodCode,
    BillingMethodName,
    BillingCustomerName,
    BillingCallingCode,
    BillingTelephone,
    BillingCountry,
    BillingCountryId,
    BillingStateId,
    BillingState,
    BillingCity,
    BillingAddress1,
    BillingAddress2,
    BillingZipcode,
    Comment,
    CreatedAt,
    UpdatedAt,
    DeletedAt,
}
